;(function ($) {
    /*
    * 保养评分
    * score('指派','null','1523343956628073','1481185324868002','null','http://gable-hospital.oss-cn-shanghai.aliyuncs.com/repair/1523343955371.png,http://gable-hospital.oss-cn-shanghai.aliyuncs.com/repair/1523343956271.png,')
    * */
    $.eventAlert = function (messageObj){
        /* 添加模态框 */
        var model = $("#event-alert");
        if (!model.length) {
            /* iziModal模态窗口:报修处理 */
            $('body').append('<div id="event-alert" class="score" style="background:#f5f5f5;">' +
                '<div class="box-body" style="overflow-y:auto;overflow-x: hidden;padding-top:0.3rem;">' +
                    '<div class="col-md-12">' +
                        '<div class="col-md-6" style="height: 100%;">' +
                            '<div class="col-md-12"><b class="text-aqua big-title">基本信息</b></div>' +
                            '<div class="col-md-6 thumb thumbnail1" style="display: none;">' +
                                '<div class="prev"><img src="' + basePath + '/images/prev.png"><span></span></div>' +
                                '<ul><li style="height: 3rem;"><img src="' + basePath + '/images/next.png"></li></ul>' +
                                '<div class="next"><img src="' + basePath + '/images/next.png"><span></span></div>' +
                            '</div>' +
                            '<div class="col-md-6">' +
                                '<p></p>' +
                                '<p><a class="bxclAssetsName big-title spanTitle" href="'+basePath+'/charts/overview/assets/page?deviceId='+messageObj.assetsId+'">' + messageObj.assetsName + '</a></p>' +
                                '<p><span class="text-muted">设备型号：</span><span class="bxclModel">' + messageObj.model + '</span></p>' +
                                '<p><span class="text-muted">设备科室：</span><span class="bxclDepartment">' + messageObj.department + '</span></p>' +
                            '</div>' +
                        '</div>' +
                        '<div class="col-md-6" style="height:100%;">' +
                            '<div class="col-md-12"><b class="text-aqua big-title">报修信息</b></div>' +
                            '<div class="col-md-6 thumb thumbnail2" style="display: none;">' +
                                '<div class="prev"><img src="' + basePath + '/images/prev.png"><span></span></div>' +
                                '<ul><li style="height: 3rem;"><img src="' + basePath + '/images/next.png"></li></ul>' +
                                '<div class="next"><img src="' + basePath + '/images/next.png"><span></span></div>' +
                            '</div>' +
                            '<div class="col-md-6">' +
                                '<p></p>' +
                                '<p><b  class="big-title">设备故障</b></p>' +
                                '<p><span class="text-muted">故障时间：</span><span class="bxclFaultTime">' + messageObj.faultTime + '</span></p>' +
                                '<p><span class="text-muted">故障描述：</span><span class="bxclFault">' + messageObj.faultContent + '</span></p>' +
                                '<p><span class="text-muted">报修平台：</span><span class="bxclRepairPlatform">' + messageObj.repairPlatform + '</span></p>' +
                            '</div>' +
                        '</div>' +
                    '</div>' +
                    '<div class="col-md-12 repair-history" style="display: none;">' +
                        '<div class="col-md-12"><b class="text-aqua col-md-12 big-title">近期维修</b></div>' +
                        '<p></p>' +
                        '<table class="table repair-history-table"><thead></thead><tbody></tbody></table>' +
                    '</div>' +
                '</div>' +
                '<div class="box-body">' +
                    '<div class="col-md-12">' +
                        '<div class="col-md-12"><b class="text-aqua col-md-12 big-title">运维情况</b></div>' +
                        '<div class="col-md-12">' +
                            '<div class="col-md-1"></div>' +
                            '<div class="col-md-4">' +
                                '<div class="chartCost" style="height: 4.8rem;width:4.8rem;"></div>' +
                            '</div>' +
                            '<div class="col-md-6" style="height: 4.8rem;padding: 1.0rem 0.4rem;">' +
                                '<label class="nowrap"><b style="font-weight: normal;">维修次数</b>&nbsp;&nbsp;&nbsp;<span class="repair-time"></span></label>' +
                                '<label class="nowrap"><b style="font-weight: normal;">强检次数</b>&nbsp;&nbsp;&nbsp;<span class="compulsory-time"></span></label>' +
                                '<label class="nowrap"><b style="font-weight: normal;">保养次数</b>&nbsp;&nbsp;&nbsp;<span class="upkeep-time"></span></label>' +
                                '<label class="nowrap"><b style="font-weight: normal;">巡检次数</b>&nbsp;&nbsp;&nbsp;<span class="inspection-time"></span></label>' +
                                '<label class="nowrap"><b style="font-weight: normal;">校正次数</b>&nbsp;&nbsp;&nbsp;<span class="correction-time"></span></label>' +
                            '</div>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
                '<div class="box-footer text-center" style="display: flex;justify-content: space-between;padding: 0 2.0rem;line-height: 0.6rem;">' +
                    '<div>' +
                        '<img class="deal prev-deal" src="' + basePath + '/images/prev.png" alt="上一个">' +
                        '<span class="progress page text-muted" style="height: auto;margin: 0.4rem 0;color:#303030;"></span>' +
                        '<img class="deal next-deal" src="' + basePath + '/images/next.png" alt="下一个">' +
                    '</div>' +
                    '<button data-iziModal-close class="btn btn-default cancel">稍后处理</button>' +
                    '<button type="button" class="btn save">立即处理</button>' +
                '</div>' +
            '</div>');
            model = $("#event-alert");
            model.iziModal({
                title: '事件提醒',
                headerColor: '#fff',
                theme: '',
                attached: '',
                icon: null,
                iconText: null,
                iconColor: '#4c4c4c',
                rtl: false,
                width: 20 * rem,
                padding: 0,
                radius: 0.4 * rem,
                zindex: 10010,
                iframe: false,
                iframeHeight: 8 * rem,
                iframeURL: null,
                focusInput: false,
                group: '',
                loop: false,
                navigateCaption: true,
                navigateArrows: true,
                history: true,
                restoreDefaultContent: false,
                autoOpen: false,
                bodyOverflow: true,
                fullscreen: false,
                openFullscreen: false,
                closeOnEscape: false,
                overlay: true,
                overlayClose: false,
                overlayColor: 'rgba(0, 0, 0, 0.4)',
                timeout: false,
                timeoutProgressbar: false,
                pauseOnHover: false,
                timeoutProgressbarColor: 'rgba(255,255,255,0.5)',
                transitionIn: 'comingIn',
                transitionOut: 'comingOut',
                transitionInOverlay: 'fadeIn',
                transitionOutOverlay: 'fadeOut',
                onFullscreen: function(){},
                onResize: function(){},
                onOpening: function(){},
                onOpened: function(){},
                onClosing: function(){},
                onClosed: function(){
                    model.addClass('score');
                    repairHistory.slideUp();
                }
            });
        }

        if (messageObj.cautionType === 1 || messageObj.cautionType === 2) {
            var type = messageObj.cautionType === 1 ? 'repair' : 'hitch';
            if (messageObj.processType === 1) {
                model.iziModal('setTitle', '<img class="blink" src="' + basePath + '/plugins/charts/images/web-modal/'+type+'.png"><span class="blink">' + messageObj.assetsName + " 设备报修 " + messageObj.repairTime + '</span>');
            } else if (messageObj.processType === 2) {
                model.iziModal('setTitle', '<span class="blink">医院确认 ' + messageObj.assetsName + '(' + messageObj.department + ')' + messageObj.repairStatus + messageObj.endTime + '</span>');
            }
        }
        model.iziModal('open');
        /* 提交 */
        if (messageObj.isDoNow) {
            model.find('.save').attr('disabled', 'disabled').off();
        } else {
            model.find('.save').off().click(function () {
                if (messageObj.processType === 1) {
                    $.appoint(messageObj.repairId, messageObj.assetsId, true, messageObj);
                } else if (messageObj.processType === 2) {
                    $.repairScore(messageObj.dispatchId, messageObj.repairId, messageObj.assetsId, messageObj.maintenanceType, 'report', true, messageObj);
                }
            });
        }

        model.find('.cancel, .iziModal-button-close').off().click(function (e){
            e.stopPropagation();
            messageObjArr[messageObjArr.index].isDoNow = true;
            messageObjArr.index++;
            if (messageObjArr.index < messageObjArr.length) {
                loading();
                $.eventAlert(messageObjArr[messageObjArr.index]);
            } else {
                messageObjArr.index = messageObjArr.length - 1;
                model.iziModal('close');
            }
        });
        /* 进度显示 */
        model.find('.progress').html(messageObjArr.index + 1 + '/' + messageObjArr.length + '条');
        /* 上一个下一个 */
        var progressEl = model.find('.progress.page');
        model.find('.deal').off().click(function () {
            var obj = $(this);
            var index = progressEl.text().split('/')[0] - 1;
            if (obj.hasClass('prev-deal')) {
                index--;
                index = index < 0 ? messageObjArr.length - 1 : index;
                messageObjArr.index = index;
                loading();
                $.eventAlert(messageObjArr[index]);
            } else if (obj.hasClass('next-deal')) {
                index++;
                index = index % messageObjArr.length;
                messageObjArr.index = index;
                loading();
                $.eventAlert(messageObjArr[index]);
            }
        });

        queryRepairView(messageObj);
        setRaderChart();
        // 查询报修信息以及设备信息
        function queryRepairView(data){
            var thumbnail1 = model.find('.thumbnail1').hide();
            if (data.assetsPhotos) {
                thumb(thumbnail1, data.assetsPhotos);
            }
            model.find('.bxclAssetsName').html(data.assetsName);
            model.find('.bxclModel').html(data.model);
            model.find('.bxclDepartment').html(data.department);

            var thumbnail2 = model.find('.thumbnail2').hide();
            if (data.repairPhotos) {
                thumb(thumbnail2, data.repairPhotos);
            }
            model.find('.bxclFaultTime').html(data.faultTime);
            if(data.faultContent){
                model.find('.bxclFault').html(data.faultContent);
            }else
            model.find('.bxclRepairPlatform').html(data.repairPlatform);
        }
        // 查询记录
        /*
            * 维修历史模态框
            * */
        var repairHistoryTable = model.find('.repair-history-table');
        var repairHistory = model.find('.repair-history');
        $.ajax({
            type: 'POST',
            url: basePath + '/repair/queryRepairAssets',
            data: {"assetsId":messageObj.assetsId}, 	// 参数设备ID
            dataType:'json',
            success:function(data){
                // 默认进来清空
                repairHistoryTable.find('thead').html("<tr>"
                    + "<th class='text-center text-muted'>故障时间</th>"
                    + "<th class='text-center text-muted'>故障描述</th>"
                    + "<th class='text-center text-muted'>更换备件</th>"
                    + "<th class='text-center text-muted'>报修时间</th>"
                    + "<th class='text-center text-muted'>修复时间</th>"
                    + "</tr>");
                var tbody = repairHistoryTable.find("tbody").html('');
                if(data==null || data==""){
                    repairHistory.slideUp();
                    tbody.append("<tr><td class='text-center text-primary' colspan='5' >无记录</td></tr>");
                }else if (data.length){
                    repairHistory.slideDown();
                    tbody.append("<tr>" +
                        "<td class='text-muted text-center'>"+fixStr(data[0].faultTime)+"</td>" +
                        "<td class='text-muted text-center'>"+fixStr(data[0].fault)+"</td>" +
                        "<td class='text-muted text-center'>"+fixStr(data[0].partName)+"</td>" +
                        "<td class='text-muted text-center'>"+fixStr(data[0].repairTime)+"</td>" +
                        "<td class='text-muted text-center'>"+fixStr(data[0].completeTime)+"</td>" +
                        "</tr>");
                    if (data.length > 1) {
                        tbody.append('<tr class="load-more" style="display: none;cursor: pointer;"><td class="text-center" style="color:#15b997;text-decoration: underline;" colspan="5">全部显示</td></tr>');
                        var loadMore = tbody.find('.load-more');
                        repairHistoryTable.hover(function () {
                            loadMore.slideDown();
                        }, function () {
                            loadMore.slideUp();
                        });
                        // 循环追加数据
                        loadMore.click(function () {
                            if (loadMore.hasClass('less')) {
                                loadMore.removeClass('less').slideUp();
                                loadMore.find('td').html('全部显示');
                                var trs = tbody.find('tr');
                                for(var i = 1; i < trs.length - 1; i++) {
                                    trs.eq(i).remove();
                                }
                            } else {
                                loadMore.addClass('less');
                                loadMore.find('td').html('显示一条');
                                for (var i = 1; i < data.length; i++) {
                                    loadMore.before("<tr>" +
                                        "<td class='text-muted text-center'>" + fixStr(data[i].faultTime) + "</td>" +
                                        "<td class='text-muted text-center'>" + fixStr(data[i].fault) + "</td>" +
                                        "<td class='text-muted text-center'>" + fixStr(data[i].partName) + "</td>" +
                                        "<td class='text-muted text-center'>" + fixStr(data[i].repairTime) + "</td>" +
                                        "<td class='text-muted text-center'>" + fixStr(data[i].completeTime) + "</td>" +
                                        "</tr>");
                                }
                            }
                        });
                    }
                }
            }
        });

        /* 设置雷达图数据 */
        function setRaderChart() {
            /* 运维情况 */
            var chartCost = echarts.init(model.find('.chartCost')[0]);
            var optionCost = {
                color: ['#d8e8f2'],
                tooltip: {},
                radar: {
                    name: {
                        textStyle: {
                            color: '#404040',
                            borderRadius: 3,
                            fontSize: 16
                        }
                    },
                    indicator: [
                        {name: '维修', max: 10},
                        {name: '强检', max: 10},
                        {name: '校正', max: 10},
                        {name: '保养', max: 10},
                        {name: '巡检', max: 10}
                    ],
                    center: ['50%', '50%'],
                    radius: '67%',
                    splitArea: {
                        areaStyle: {
                            color: [],
                            shadowColor: '#10458f',
                            shadowBlur: 10
                        }
                    }
                },
                series: [{
                    name: '运行情况',
                    type: 'radar',
                    symbol: 'roundRect',
                    symbolSize: '1',
                    itemStyle: {normal: {areaStyle: {type: 'default',opacity: 0.1}}},
                    lineStyle: {
                        normal: {
                            color: "#65a3ca"
                        }
                    },
                    data: [
                        {
                            value: [10, 10, 10, 10, 10],
                            name: '计划值',
                            lineStyle: {
                                normal: {
                                    color: '#6da953'
                                }
                            },
                            areaStyle: {
                                normal: {
                                    opacity: 0
                                }
                            }
                        },
                        {
                            value: [],
                            name: '运行情况'
                        }
                    ]
                }]
            };
            var planUpkeepCounts = 10,
                realUpkeepCounts = 0,
                planInspectionCounts = 10,
                realInspectionCounts = 0,
                plancompulsoryCounts = 10,
                realcompulsoryCounts = 0,
                correctionCounts = 10,
                repairCounts = 0,
                maxRepairCounts = 10;
            $.ajax({
                url: basePath + '/charts/overview/assets/fields',
                type: 'POST',
                dataType: 'json',
                data: {deviceId: messageObj.assetsId},
                success: function (data) {
                    if (data.ret) {
                        planUpkeepCounts = parseInt(data.data.radar.planUpkeepCounts || 0);
                        realUpkeepCounts = parseInt(data.data.radar.realUpkeepCounts || 0);
                        planUpkeepCounts = planUpkeepCounts < realUpkeepCounts ? realUpkeepCounts : planUpkeepCounts;
                        planInspectionCounts = parseInt(data.data.radar.planInspectionCounts || 0);
                        realInspectionCounts = parseInt(data.data.radar.realInspectionCounts || 0);
                        planInspectionCounts = planInspectionCounts < realInspectionCounts ? realInspectionCounts : planInspectionCounts;
                        plancompulsoryCounts = parseInt(data.data.radar.plancompulsoryCounts || 0);
                        realcompulsoryCounts = parseInt(data.data.radar.realcompulsoryCounts || 0);
                        plancompulsoryCounts = plancompulsoryCounts < realcompulsoryCounts ? realcompulsoryCounts : plancompulsoryCounts;
                        correctionCounts = parseInt(data.data.radar.correctionCounts || 0);
                        repairCounts = parseInt(data.data.radar.toRepairCounts || 0);
                        maxRepairCounts = parseInt(data.data.radar.maxRepairCounts || 0);
                        maxRepairCounts = maxRepairCounts < repairCounts ? repairCounts : maxRepairCounts;
                    }
                    model.find('.repair-time').text(repairCounts + '次');
                    model.find('.upkeep-time').text(realUpkeepCounts + '次');
                    model.find('.compulsory-time').text(realcompulsoryCounts + '次');
                    model.find('.inspection-time').text(realInspectionCounts + '次');
                    model.find('.correction-time').text(correctionCounts + '次');

                    optionCost.radar.indicator[0].max = maxRepairCounts;
                    optionCost.radar.indicator[1].max = plancompulsoryCounts;
                    optionCost.radar.indicator[2].max = correctionCounts;
                    optionCost.radar.indicator[3].max = planUpkeepCounts;
                    optionCost.radar.indicator[4].max = planInspectionCounts;
                    optionCost.series[0].data[0].value = [maxRepairCounts, plancompulsoryCounts, correctionCounts, planUpkeepCounts, planInspectionCounts];
                    optionCost.series[0].data[1].value = [repairCounts, realcompulsoryCounts, correctionCounts, realUpkeepCounts, realInspectionCounts];
                    chartCost.clear();
                    chartCost.setOption(optionCost);
                    loading(true);
                }
            });
        }

        /* 缩略图 */
        function thumb(thumbnail2, assetsPhotos) {
            var assetsPhotos = assetsPhotos.split(',');
            if (!assetsPhotos[assetsPhotos.length - 1]) {
                assetsPhotos.pop();
            }
            thumbnail2.find('ul img').attr('src', assetsPhotos[0]).viewer({zIndex:10011});
            var prev = thumbnail2.find('.prev').css('visibility', 'hidden');
            var next = thumbnail2.find('.next').css('visibility', 'hidden');
            var index = 0;
            if (assetsPhotos.length > 1) {
                next.css('visibility', 'visible');
            }
            next.click(function (){
                index++;
                index %= assetsPhotos.length;
                if (index === assetsPhotos.length - 1) {
                    next.css('visibility', 'hidden');
                    prev.css('visibility', 'visible');
                } else if (index > 0) {
                    prev.css('visibility', 'visible');
                }
                thumbnail2.find('ul img').attr('src', assetsPhotos[index]);
            });
            prev.click(function (){
                index--;
                index = index < 0 ? assetsPhotos.length - 1 : index;
                if (index === 0) {
                    prev.css('visibility', 'hidden');
                    next.css('visibility', 'visible');
                } else if (index < assetsPhotos.length - 1) {
                    next.css('visibility', 'hidden');
                }
                thumbnail2.find('ul img').attr('src', assetsPhotos[index]);
            });
            thumbnail2.show();
        }
    };
})(jQuery);